package com.eendtech.androidthreadtest;

import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    TextView textView ;
    Button btn_changeUI;
    private final int UPDATE_TEXT = 0 ;
    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case UPDATE_TEXT:
                    //在Hnadler中进行UI操作
                    textView.setText("在这里我们进行了UI操作");
                    break;
            }

        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }
    public void initView (){
        //找到视图
        textView = (TextView) findViewById(R.id.textView);
        btn_changeUI = (Button)findViewById(R.id.btn_changeUI);
        //定义监听内容
        btn_changeUI.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //构建一个线程来发送message
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        //构建message并且发送
                        Message msg = new Message();
                        msg.what = UPDATE_TEXT;
                        handler.sendMessage(msg);
                    }
                }).start();
            }
        });
    }


}
